package com.hinnotek.warehouse.ui

import android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.hinnotek.warehouse.R
import com.hinnotek.warehouse.databinding.ActivityModifyPassBinding
import com.hinnotek.warehouse.viewmodel.LoginViewModel
import com.lee.common.base.BaseActivity
import com.lee.common.observer.DefaultObserver
import com.lee.common.utils.ACTIVITY_MODIFY_PASS
import com.lee.common.utils.ToastUtils
import dagger.hilt.android.AndroidEntryPoint

@Route(path = ACTIVITY_MODIFY_PASS)
@AndroidEntryPoint
class ModifyPassActivity : BaseActivity<ActivityModifyPassBinding>() {

    private val viewModel by viewModels<LoginViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_modify_pass)
        setTitle("修改密码")
    }

    override fun initVew() {

        bindingView.modifyModel = viewModel
        bindingView.executePendingBindings()
        viewModel.attachLoading(loadingState)
        viewModel.dialogMsg.observe(this, DefaultObserver(this))

        bindingView.save.setOnClickListener {

            val pass2 = viewModel.pass2.get() ?: ""
            val pass3 = viewModel.pass3.get() ?: ""
            if (pass2.length < 6) {
                ToastUtils.showShort("请输入大于6位字符密码")
                return@setOnClickListener
            }
            if (pass2 != pass3) {
                ToastUtils.showShort("两次密码不一致!")
                return@setOnClickListener
            }

            viewModel.loadData()

        }
    }

    override fun setData() {

        viewModel.modifyPassPage.observe(this, Observer {
            if (viewModel.isSuccess) {
                ToastUtils.showLong("修改成功")
                finish()
            }
        })
    }
}